/**
 * Copyright (c) 2015-2017, Henry Yang 杨勇 (gismail@foxmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mapfinal.server.auth.web;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import com.lambkit.web.RequestManager;
import com.lambkit.web.tag.LambkitTemplateModel;
import com.mapfinal.server.auth.AuthLoginCache;
import com.mapfinal.server.auth.model.AuthJob;
import com.mapfinal.server.auth.model.AuthOrganization;
import com.mapfinal.server.auth.model.AuthRule;
import com.mapfinal.server.auth.model.Role;
import com.mapfinal.server.auth.model.User;
import com.jfinal.render.FreeMarkerRender;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * @author yangyong 
 * @website: www.lambkit.com
 * @email: gismail@foxmail.com
 * @date 2020-09-24
 * @version 1.0
 * @since 1.0
 */
/**
 * Auth标签, 当前用户的信息，role， organization，rule， job<br>
 * 返回值：{entity信息}
 * @author lambkit
 */
public class AuthMarker extends LambkitTemplateModel {

	@Override
	public void onRender(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		// TODO Auto-generated method stub
		String type = get(params, "type");
		String tagEntityKeyname = get(params, "key", "entity");
		HttpSession session = RequestManager.me().getRequest().getSession(false);
		String userName = session != null ? (String)session.getAttribute("user") : null;
		User user = AuthLoginCache.getLoginUser(userName);
		if(user==null) {
			env.setVariable(tagEntityKeyname, null);
		} else if("role".equals(type)) {
			List<Role> roles = Role.service().getRoles(user.getId());
			env.setVariable(tagEntityKeyname, FreeMarkerRender.getConfiguration().getObjectWrapper().wrap(roles));
		} else if("organization".equals(type)) {
			List<AuthOrganization> orgs = AuthOrganization.service().getOrganizations(user.getId());
			env.setVariable(tagEntityKeyname, FreeMarkerRender.getConfiguration().getObjectWrapper().wrap(orgs));
		} else if("rule".equals(type)) {
			List<AuthRule> rules = AuthRule.service().getPermissions(user.getId());
			env.setVariable(tagEntityKeyname, FreeMarkerRender.getConfiguration().getObjectWrapper().wrap(rules));
		} else if("job".equals(type)) {
			List<AuthJob> jobs = AuthJob.service().getJobs(user.getId());
			env.setVariable(tagEntityKeyname, FreeMarkerRender.getConfiguration().getObjectWrapper().wrap(jobs));
		} else {
			env.setVariable(tagEntityKeyname, FreeMarkerRender.getConfiguration().getObjectWrapper().wrap(user));
		}
        body.render(env.getOut());
	}
}
